package org.itboys.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.itboys.admin.common.SystemLoginUserPermission;
import org.itboys.admin.service.AdminUserService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.interceptor.ServletContextInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by weisky on 2016/8/11.
 */
public class AdminIntercepter extends ServletContextInterceptor {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	/**
	 * 是否开启接口的权限校验。
	 * 开启。自行删除后台角色管理页面。template\screen\ace\sys\role-list.vm中【分配菜单权限】按钮
	 * 关闭。自行删除后台角色管理页面。template\screen\ace\sys\role-list.vm中【分配功能权限】按钮
	 */
	public static boolean is_open_operate_permission_check=false;
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private AdminUserService adminUserService;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {

        request.setAttribute("staticRoot", resourceHolder.getStringValue("staticRoot"));
        request.setAttribute("adminRoot", resourceHolder.getStringValue("adminRoot"));
        request.setAttribute("imgRoot", resourceHolder.getStringValue("imgRoot"));

        //2017年3月14日，江雄
		request.setAttribute("dictionaryRoot", resourceHolder.getStringValue("dictionaryRoot"));
		request.setAttribute("dictionaryRootMuilt", resourceHolder.getStringValue("dictionaryRootMuilt"));



		String url = request.getRequestURI();
		logger.info("-------admin-执行拦截器0-----当前请求地址"+url);
		 getAllParam(request);  
		/**校验接口权限**/
		if(is_open_operate_permission_check){
			System.err.print("本次用户权限"+ ListUtil.isNotNull(SystemLoginUserPermission.loginUserOperatePermssion));
			if(ListUtil.isNotNull(SystemLoginUserPermission.loginUserOperatePermssion)) {
				for (SystemLoginUserPermission per : SystemLoginUserPermission.loginUserOperatePermssion) {


				}

		}
		}
        return super.preHandle(request, response, handler);
    }





    public static String getAllParam(HttpServletRequest request){
		Map<String,String[]> params=request.getParameterMap();
		
		 String allParam = "";  
		
	        for (String key : params.keySet()) {  
	            String[] values = params.get(key);  
	            
	            for (int i = 0; i < values.length; i++) {  
	                String value = values[i];  
	                allParam += key + "=" + value + "&";  
	            }  
	        }  
	        // 去掉最后一个空格  
	        if(StringUtils.isNotEmpty(allParam)){
	        	allParam = allParam.substring(0, allParam.length() - 1); 
	        }
	        System.out.println("admin拦截器-请求参数-----"+allParam);
	        return allParam;
	}
}
